package com.tfswx.fljdmxyy.validation;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.HashMap;
import java.util.Map;

/**
 * 文件类型校验
 *
 * @author root
 * @date 2022-02-21
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = FileValid.Validator.class)
public @interface FileValid {
    String[] value();

    long maxSize() default -1;

    String message() default "{path}-{errmsg}";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};


    class Validator implements ConstraintValidator<FileValid, MultipartFile> {
        private String[] values;
        private long maxSize;

        private static String getSizeStr(long size, boolean sfjq) {
            class A {
                String[] dws = {"B", "KB", "MB", "GB", "TB"};

                String getSizeStr(long size, boolean sfjq, int dw) {
                    double l = size / 1024.0;
                    if (l > 1) {
                        if (sfjq) {
                            return getSizeStr(size / 1024, sfjq, dw + 1) + size % 1024 + dws[dw];
                        } else {
                            return getSizeStr(size / 1024, sfjq, dw + 1);
                        }
                    }
                    return size % 1024 + dws[dw];
                }
            }
            A a = new A();
            return a.getSizeStr(size, sfjq, 0);
        }

        @Override
        public void initialize(FileValid constraintAnnotation) {
            values = constraintAnnotation.value();
            maxSize = constraintAnnotation.maxSize();
        }

        @Override
        public boolean isValid(MultipartFile file, ConstraintValidatorContext context) {
            if (file == null) {
                return true;
            }
            String originalFilename = file.getOriginalFilename();
            if (StringUtils.isBlank(originalFilename)) {
                Map<String, String> map = new HashMap<>(1);
                map.put("errmsg", "上传文件必须有文件名称");
                ValidUtil.setParam(context, map);
                return false;
            }

            if (maxSize > 0) {
                long size = file.getSize();
                if (size > maxSize) {
                    Map<String, String> map = new HashMap<>(1);
                    map.put("errmsg", "文件大小不能大于" + getSizeStr(maxSize, false));
                    ValidUtil.setParam(context, map);
                    return false;
                }
            }

            String s = FilenameUtils.getExtension(originalFilename);
            for (String value1 : values) {
                if (s.equalsIgnoreCase(value1)) {
                    return true;
                }
            }

            Map<String, String> map = new HashMap<>(1);
            map.put("errmsg", "文件类型不支持，请上传" + values + "类型的文件");
            ValidUtil.setParam(context, map);
            return false;
        }
    }
}
